package org.kpli.jak.second;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 
 * @author dendy
 * 
 */
public class Client {

	// membuat port
	private final int PORT = 50000;

	// membuat ip
	private final String URL = "localhost";

	// membuat statement untuk server
	private final String QUIT = "QUIT";

	// mengambil input dari client
	BufferedReader inputFormClient = null;

	DataOutputStream dataOutputStream = null;

	// mengambil input dari server
	BufferedReader inputFormServer = null;

	// Socket sebagai penghubung dari server ke client
	Socket socket = null;

	public Client() {

		try {
			// meminta input dari user
			inputFormClient = new BufferedReader(new InputStreamReader(
					System.in));
			/*
			 * Socket berguna untuk client sedangkan ServerSocket berguna untuk
			 * server
			 */
			socket = new Socket(URL, PORT);
			dataOutputStream = new DataOutputStream(socket.getOutputStream());
			// mendapatkan input dari server
			inputFormServer = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			boolean quit = false;
			while (!quit) {
				System.out.println("Untuk Keluar Masukan Command QUIT \n");
				System.out.println("Silahkan Masukan perintah: ");
				String commandClient = inputFormClient.readLine();
				if (commandClient.equals(QUIT)) {
					quit = true;
					System.out.println("Koneksi telah terputus,BYE...");
				}
			}
			inputFormClient.close();
			socket.close();
			inputFormServer.close();
		} catch (UnknownHostException e) {
			System.out.println("Error from Server " + e);
		} catch (IOException e) {
			System.out.println("Error from Server " + e);
		}
	}
}
